﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using TSqlServer;
public partial class adsetup_TAdSetupInfo2 : System.Web.UI.Page
{
    public int pagesize = 20;
    public string pager = "";
    public string typename = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //TUser1.CheckUser(this);
            if (Request["typeid"] != null)
            {
                TAdSetupInfo1 tasi1 = new TAdSetupInfo1(Request["typeid"].ToString());
                tasi1.Select();
                typename = tasi1.TypeName; 
                spanlab.InnerText = Request["typeid"].ToString();
                txtInfoId.Text = Request["typeid"].ToString();
            }
            Bind();
            if (Request["opration"] != null)
                switch (Request["opration"].ToString())
                {
                    case "bind":
                        BindInput();
                        break;
                    case "edit":
                        Save("edit");
                        break;
                    case "save":
                        Save("save");
                        break;
                    case "del":
                        Del();
                        break;



                }
        }
       
       
    }
   
    public void Bind()
    {
        string sSql = "";
        int page = 1;
        if (Request["page"] != null)
        {
            page = int.Parse(Request["page"].ToString());
            sSql = "with a as( select (select count(*) from t_ad_setup_info2) as countnum,row_number() over(order by a.orderby desc) as num,a.*,b.typename from t_ad_setup_info2 as a  left join t_ad_setup_info1 as b on b.typeid=a.typeid where a.typeid='" + spanlab.InnerText + "' ) select  * from a where num between " + ((page - 1) * pagesize + 1) + " and " + ((page - 1) * pagesize + pagesize);
        }
        else
        {
            sSql = "with a as( select (select count(*) from t_ad_setup_info2) as countnum,row_number() over(order by a.orderby desc) as num,a.*,b.typename from t_ad_setup_info2 as a  left join t_ad_setup_info1 as b on b.typeid=a.typeid where a.typeid='" + spanlab.InnerText + "' ) select  * from a where num between 1 and " +  pagesize ;
        }
        TSqlDBReader myReader = new TSqlDBReader(sSql);
        dlTable.DataSource = myReader.FDataReader;
        dlTable.DataBind();
        if (myReader.FDataReader.HasRows)
        {
            int countnum = TSqlDB.ExecuteScalar(sSql);
            if (countnum % pagesize > 0)
                pager = GenPagebar(page, countnum / pagesize + 1);
            else
                pager = GenPagebar(page, countnum / pagesize);
        }
        

       
    }

    public void Save(string type)
    {

        TAdSetupInfo2 oASI2 = new TAdSetupInfo2(Request["id"].ToString());
        oASI2.Select();
        oASI2.InfoName  = Request["name"].ToString();
        if (Request["show"].ToString() == "true")
            oASI2.IsShow = "1";
        else
            oASI2.IsShow = "0";
        oASI2.OrderBy = decimal.Parse(Request["order"].ToString());
        oASI2.TypeId = Request["type"].ToString();
        if (type == "edit")
        {
            oASI2.Update();
        }
        else
        {
            if (oASI2.InfoId  == null)
            {
                oASI2.InfoId  = Request["id"].ToString();
                oASI2.Insert();
            }
            else
            {
                Response.Clear();
                Response.Write("代码重复");
                Response.Flush();
                Response.Close();
                Bind();
                return;
            }
        }
        Response.Clear();
        Response.Flush();
        Response.Close();
       



    }
    public void BindInput()
    {
        if (Request["id"] != null)
        {
            TAdSetupInfo2 oASI2 = new TAdSetupInfo2(Request["id"].ToString());
            oASI2.Select();
            Response.Clear();
            Response.Write(oASI2.InfoId + "," + oASI2.InfoName + "," + oASI2.IsShow  + "," + oASI2.OrderBy  + "," + oASI2.TypeId  );
            Response.Flush();
            Response.Close();
        }
    }
    public void Del()
    {
        if (Request["id"] != null)
        {
            TAdSetupInfo2 oASI2 = new TAdSetupInfo2(Request["id"].ToString());
            oASI2.Delete();
        }
      
        Bind();

    }


    protected void dlTable_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Del")
        {
            TAdSetupInfo2 oASI2 = new TAdSetupInfo2(e.CommandArgument.ToString());
            oASI2.Delete();
        }
        TAdSetupInfo1 tasi1 = new TAdSetupInfo1(Request["typeid"].ToString());
        tasi1.Select();
        typename = tasi1.TypeName; 
        Bind();
    }

    public string GenPagebar(int PageNum, int PageCounts)
    {
        string result = "<div class=\"pagebox\">";


        if (PageCounts > 1)
        {
            if (PageNum == 1)//第一页
            {
                result += "<span class=\"pagebox_num_nonce\"><a>首页</a></span>";
                for (int i = 1; i <= PageCounts; i++)
                {
                    if (i != PageNum)
                    {
                        result += "<span class=\"pagebox_num\"><a href='TAdSetupInfo2.aspx?typeid=" + Request["typeid"].ToString() + "&page=" + i.ToString() + "'\">" + i.ToString() + "</a></span>";
                    }
                    else
                    {
                        result += "<span class=\"pagebox_num_nonce\"><a>" + i.ToString() + "</a></span>";
                    }
                }
                result += "<span class=\"pagebox_pre_nolink\">上一页</span>";
                result += "<span class=\"pagebox_next\"><a href='TAdSetupInfo2.aspx?typeid=" + Request["typeid"].ToString() + "&page=2'\">下一页</a></span>";
                result += "<span class=\"pagebox_num\"><a href='TAdSetupInfo2.aspx?typeid=" + Request["typeid"].ToString() + "&page=" + PageCounts.ToString() + "'\">尾页</a></span>";
            }
            if ((PageNum > 1) && (PageNum < PageCounts))
            {
                result += "<span class=\"pagebox_num\"><a href='TAdSetupInfo2.aspx?typeid=" + Request["typeid"].ToString() + "&page=1' \">首页</a></span>";
                for (int i = 1; i <= PageCounts; i++)
                {
                    if (i != PageNum)
                    {
                        result += "<span class=\"pagebox_num\"><a href='TAdSetupInfo2.aspx?typeid=" + Request["typeid"].ToString() + "&page=" + i.ToString() + "'\">" + i.ToString() + "</a></span>";
                    }
                    else
                    {
                        result += "<span class=\"pagebox_num_nonce\"><a>" + i.ToString() + "</a></span>";
                    }
                }
                result += "<span class=\"pagebox_pre\"><a href='TAdSetupInfo2.aspx?typeid=" + Request["typeid"].ToString() + "&page=" + (PageNum - 1).ToString() + "'\">上一页</a></span>";
                result += "<span class=\"pagebox_next\"><a href='TAdSetupInfo2.aspx?typeid=" + Request["typeid"].ToString() + "&page=" + (PageNum + 1).ToString() + "'\">下一页</a></span>";
                result += "<span class=\"pagebox_num\"><a href='TAdSetupInfo2.aspx?typeid=" + Request["typeid"].ToString() + "&page=" + PageCounts.ToString() + "'\">尾页</a></span>";
            }

            if (PageNum == PageCounts)//最后一页
            {
                result += "<span class=\"pagebox_num\"><a href='TAdSetupInfo2.aspx?typeid=" + Request["typeid"].ToString() + "&page=1'\">首页</a></span>";
                for (int i = 1; i <= PageCounts; i++)
                {
                    if (i != PageNum)
                    {
                        result += "<span class=\"pagebox_num\"><a href='TAdSetupInfo2.aspx?typeid=" + Request["typeid"].ToString() + "&page=" + i.ToString() + "'\">" + i.ToString() + "</a></span>";
                    }
                    else
                    {
                        result += "<span class=\"pagebox_num_nonce\"><a>" + i.ToString() + "</a></span>";
                    }
                }
                result += "<span class=\"pagebox_pre\"><a href='TAdSetupInfo2.aspx?typeid=" + Request["typeid"].ToString() + "&page=" + (PageCounts - 1).ToString() + "'\">上一页</a></span>";
                result += "<span class=\"pagebox_next_nolink\">下一页</span>";
                result += "<span class=\"pagebox_num_nonce\"><a>尾页</a></span>";
            }
        }
        else
        {
            result += "<span class=\"pagebox_num_nonce\"><a>首页</a></span>";
            result += "<span class=\"pagebox_num_nonce\"><a>1</a></span>";
            result += "<span class=\"pagebox_pre_nolink\">上一页</span>";
            result += "<span class=\"pagebox_next_nolink\">下一页</span>";
            result += "<span class=\"pagebox_num_nonce\"><a>尾页</a></span>";
        }

        result += "<span style=\"padding-top: 4px;\">&nbsp;(每页显示" + pagesize.ToString() + "行)</span>";
        result += "</div>";

        return result;
    }

    protected void btnPanel2Cancel_Click(object sender, EventArgs e)
    {
        TAdSetupInfo1 tasi1 = new TAdSetupInfo1(Request["typeid"].ToString());
        tasi1.Select();
        typename = tasi1.TypeName; 
        Bind();
    }
}
